package easier.popup.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 展示可选择数据
 */
public class PopupCheckFactory extends PopupWindowFactory {

    protected final RecyclerView mRecycler;

    public PopupCheckFactory( Context context ) {

        View view = LayoutInflater.from( context ).inflate( R.layout.popup_window_check_recycler, null );

        mRecycler = view.findViewById( R.id.recycler );
        mRecycler.setLayoutManager( new LinearLayoutManager( context ) );

        mWindow = new PopupWindow( view );
        onWindowCreate( mWindow );
    }

    @Override
    public void dismiss() {

        super.dismiss();
        mRecycler.setAdapter( null );
    }

    /**
     * @param anchor 锚点
     * @param items  显示数据
     */
    public void showAsDropDown( View anchor, List< Item > items ) {

        mRecycler.setAdapter( new Adapter( items ) );

        showAsDropDown( anchor );
    }

    public static class Item {

        private String id;
        private String value;
        private boolean checked;

        public Item( String id, String value, boolean checked ) {

            this.id = id;
            this.value = value;
            this.checked = checked;
        }

        public String getId() {

            return id;
        }

        public void setId( String id ) {

            this.id = id;
        }

        public String getValue() {

            return value;
        }

        public void setValue( String value ) {

            this.value = value;
        }

        public boolean isChecked() {

            return checked;
        }

        public void setChecked( boolean checked ) {

            this.checked = checked;
        }
    }

    /**
     * @param parent 父布局
     *
     * @return 创建显示布局
     */
    protected View onCreateItemView( @NonNull ViewGroup parent ) {

        return LayoutInflater.from( parent.getContext() ).inflate( R.layout.popup_window_check_recycler_item_1, parent, false );
    }

    /**
     * 绑定数据到布局
     *
     * @param itemView 布局
     * @param item     数据
     */
    protected void onItemViewBind( View itemView, Item item ) {

        ((TextView) itemView.findViewById( R.id.text )).setText( item.value );
    }

    /**
     * @param item 回调通知选中
     */
    protected void onItemChecked( Item item ) {

    }

    /**
     * recycler adapter
     */
    protected class Adapter extends RecyclerView.Adapter< Holder > {

        private final List< Item > items;

        public Adapter( List< Item > items ) {

            this.items = items;
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {

            View view = onCreateItemView( parent );
            return new Holder( view );
        }

        @Override
        public void onBindViewHolder( @NonNull Holder holder, int position ) {

            holder.bind( items.get( position ) );
        }

        @Override
        public int getItemCount() {

            return items.size();
        }
    }

    protected class Holder extends RecyclerView.ViewHolder {

        private Item mItem;

        public Holder( @NonNull View itemView ) {

            super( itemView );
            itemView.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    onItemChecked( mItem );
                    dismiss();
                }
            } );
        }

        private void bind( Item item ) {

            mItem = item;
            onItemViewBind( itemView, item );
        }
    }

    /**
     * 空选择界面
     */
    public static class PopupCheckEmpty extends PopupCheckFactory {

        public PopupCheckEmpty( Context context ) {

            super( context );
        }

        @Override
        protected View onCreateItemView( @NonNull ViewGroup parent ) {

            return LayoutInflater.from( parent.getContext() ).inflate(
                R.layout.popup_window_check_recycler_item_empty, parent, false
            );
        }

        @Override
        protected void onItemViewBind( View itemView, Item item ) {}

        @Override
        protected void onItemChecked( Item item ) {

            super.onItemChecked( item );
        }

        @Override
        public void showAsDropDown( View anchor ) {

            List< Item > items = new ArrayList<>( 1 );
            items.add( new Item( "", "", false ) );

            showAsDropDown( anchor, items );
        }

        @Override
        public void showAsDropDown( View anchor, List< Item > items ) {

            mRecycler.setAdapter( new Adapter( items ) );
            showAsDropDown( anchor, 0, 0 );
        }
    }
}